/**
 * 排行榜管理器
 */
class RankManager {
	public constructor() {
	}

	private static _instance:RankManager;
	public static instance():RankManager{
		if(!RankManager._instance){
			RankManager._instance = new RankManager();
		}
		return RankManager._instance;
	}

	private _worldUserData:Dictionary<any[]> = new Dictionary<any[]>();
	public get worldUserData(){
		return this._worldUserData;
	}
	public addUserInfo(value:any, key:number){
		let list = this._worldUserData.getValueByKey(key);
		if(!list){
			list = [];
			this._worldUserData.add(key, list);
		}
		list.push(value);
	}

	/**
	 * 上传用户分数到微信后台
	 */
	public uploadUserScore(score:number, type:string){
		let value = JSON.stringify({
				wxgame:{
					score:score,
					update_time:TimeUtil.getTimeStamp()
				},
				cost_ms:1000
			});

        WeChatgGameManager.instance().setUserDataToCould([{
			key:type,
			value:value
		}]);
	}

	/**
	 * 请求排行榜数据
	 */
	public postRankData(page:number = 1,type:number = 0, call?, scope?){
		UIManager.instance().showNormalLoading();
		let openid = RoleCache.instance().openid ? RoleCache.instance().openid : '';
        let data = {
            openid : openid,
			page: page,
			type: type
        }
		let dic = this._worldUserData;
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishingaccs/get_rank' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            if(call)
            {
				let arr = data.data
				for(let k in data.data){
					this.addUserInfo(arr[k], type);
				}
				
				let parma = {
					data : dic.getValueByKey(type),
					type : type
				}

                call.call(scope, parma);
            }
        })
	}

	/**
	 * 请求自己的数据
	 */	
	public postSelfRankData(type:number = 0, call?, scope?)
	{
		UIManager.instance().showNormalLoading();
		let openid = RoleCache.instance().openid ? RoleCache.instance().openid : '';
        let data = {
            openid : openid,
			type: type
        }
		let dic = this._worldUserData;
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishingaccs/get_self_rank' ,StringUtil.jsonToHttpPostString(data), (e)=>{
            UIManager.instance().hideNormalLoading();
            let data = JSON.parse(e);
            if(call)
            {
                call.call(scope, data.data);
            }
        })
	}

	/**
	 * 更新玩过的微信好友数据
	 */
	public loadFriendCloudStorage(){
        WeChatgGameManager.instance().sendMessageToOpen({
            command:'loadRes'
        });
	}

	/**
	 * 更新玩过的微信好友数据
	 */
	public updateFriendCloudStorage(){
        WeChatgGameManager.instance().sendMessageToOpen({
            command:'open'
        });
	}

	/**
	 * 更新玩过的微信好友数据
	 */
	public closeFriendCloudStorage(){
        WeChatgGameManager.instance().sendMessageToOpen({
            command:'close'
        });
	}
}